package com.android.fastride.view.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.fastride.R;
import com.android.fastride.contract.MessageListContract;
import com.android.fastride.model.db.entity.FastRideMessage;
import com.android.fastride.present.MessageListPresent;
import com.android.fastride.view.widget.TopBar;
import com.chad.library.adapter.base.BaseViewHolder;
import com.peng.library.mantis.expand.list.MantisListFragment;
import com.peng.library.mantis.expand.list.link.ListConfig;
import com.peng.library.mantis.expand.refresh.link.RefreshConfig;
import com.peng.library.mantis.hint.HintConfig;
import com.peng.library.mantis.mvp.BindPresent;
import com.peng.library.mantis.mvp.IViewDelegate;

import java.util.Calendar;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

@BindPresent(MessageListPresent.class)
public class MesssageCenterListFragment extends MantisListFragment<MessageListContract.PRESENT,
        FastRideMessage.NoticeBean>
        implements MessageListContract.VIEW {
    Unbinder unbinder;
    @BindView(R.id.tb_top_bar)
    TopBar tbTopBar;
    @BindView(R.id.rv_message_list)
    RecyclerView rvMessageList;

    @Override
    public MessageListContract.PRESENT getPresent() {
        return super.getPresent();
    }

    @Override
    public View onCreateView(LayoutInflater inflater,
                             @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState,
                             HintConfig.Builder hintConfigBuilder,
                             RefreshConfig.Builder refreshConfigBuilder,
                             ListConfig.Builder listConfigBuilder) {

       /* hintConfigBuilder.layoutEmptyId(R.layout.hint_file_empty);
        View contentView = inflater.inflate(R.layout.fragment_collect_device_list, container,
        false);
        ((CustomerTitleBar)contentView.findViewById(R.id.title_bar)).setOnTitleBarClickListener
        (this); // 设置点击事件
        listConfigBuilder.recyclerViewId(R.id.recycle_view);
        refreshConfigBuilder.needRefreshViewId(R.id.recycle_view);
        listConfigBuilder.hasDivider(false);
        *//*listConfigBuilder.dividerColor(Color.TRANSPARENT);
        listConfigBuilder.dividerHeightDp(Kits.Dimens.dpToPx(getContext(), 5f));*//*
//        listConfigBuilder.canLoadMore(true);
        refreshConfigBuilder.refreshEnable(true);
        refreshConfigBuilder.autoRefresh(true);*/
        View view = inflater.inflate(R.layout.fragment_message_center, container, false);
        unbinder = ButterKnife.bind(this, view);
        hintConfigBuilder.layoutEmptyId(R.layout.hint_empty);
        refreshConfigBuilder.needRefreshViewId(R.id.rv_message_list);
        refreshConfigBuilder.refreshEnable(true);
        refreshConfigBuilder.autoRefresh(true);
        refreshConfigBuilder.pullToRefresh(true);
        listConfigBuilder.recyclerViewId(R.id.rv_message_list);
        listConfigBuilder.canLoadMore(true);
        listConfigBuilder.hasDivider(false);

        initView();
        return view;
    }

    private void initView() {

        tbTopBar.setOnLeftAndRightClickListener(new TopBar.OnLeftAndRightClickListener() {
            @Override
            public void OnLeftButtonClick() {
                getActivity().finish();
            }

            @Override
            public void OnRightButtonClick() {

            }
        });
    }

    @Override
    public void onRetry(int type, Object tag) {
        super.onRetry(type, tag);
        getPresent().onRefreshCall();
        getViewDelegate().showProgressView();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (unbinder != null) {
            unbinder.unbind();
            unbinder = null;
        }
    }

    @Override
    public IViewDelegate getViewDelegate() {
        return null;
    }

    @Override
    public void onConvert(BaseViewHolder holder, FastRideMessage.NoticeBean noticeBean, int
            position) {
        TextView messageTitle = (TextView) holder.getView(R.id.message_title);
        TextView publicData = (TextView)holder.getView(R.id.tv_date);
        TextView content = (TextView)holder.getView(R.id.tv_content);
        messageTitle.setText(noticeBean.getTitle());
        Calendar calendar =Calendar.getInstance();
        calendar.setTimeInMillis(noticeBean.getCreatedAt());
        publicData.setText(calendar.get(Calendar.YEAR) + "-"
                + calendar.get(Calendar.MONTH) + "-"
                + calendar.get(Calendar.DAY_OF_MONTH));
        content.setText(noticeBean.getContent());
    }

    @Override
    public void onReceiveError(Throwable throwable) {

    }

    @Override
    public int getItemLayout() {
        return R.layout.list_item_message;
    }
}
